package com.saclub.app.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost.OnTabChangeListener;
import butterknife.Bind;
import butterknife.OnClick;

import com.offroader.utils.ViewUtils;
import com.saclub.app.R;
import com.saclub.app.activity.MyToolBarActivity;
import com.saclub.app.common.AccessTokenKeeper;
import com.saclub.app.common.MyPageHelper;

/**
 * 我的
 * 
 * @author li.li
 *
 */
public class AccountFragment extends BaseProgressFragment<MyToolBarActivity> implements OnTabChangeListener {

	@Bind(R.id.id_user_login_action_layout)
	protected View loginActionLayout;

	@Bind(R.id.id_user_logout_action_layout)
	protected View logoutActionLayout;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return super.onCreateView(inflater, container, savedInstanceState);
	}


	@Override
	public void onResume() {
		super.onResume();

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {
			loginActionLayout.setVisibility(View.GONE);
			logoutActionLayout.setVisibility(View.VISIBLE);
		} else {
			loginActionLayout.setVisibility(View.VISIBLE);
			logoutActionLayout.setVisibility(View.GONE);
		}
	}

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_account;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {
	}

	@Override
	public void onTabChanged(String tabId) {

	}

	@OnClick(R.id.id_user_edit_btn)
	public void goToUserEdit() {
		AccountEditFragment.go(getActivity());
	}

	@OnClick(R.id.id_user_favorite_btn)
	public void goMyFavorite() {

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {
			AccountMyFavFragment.go(getActivity());
		} else {
			ViewUtils.toast(R.string.tip_need_login);
			AccountLoginFragment.go(getActivity());
		}

	}

	@OnClick(R.id.id_user_contact_us_btn)
	public void goContactUs() {
		AccountContactUsFragment.go(getActivity());
	}

	@OnClick(R.id.id_user_service_right_btn)
	public void goServiceRight() {
		AcountServiceRightFragment.go(getActivity());
	}

	@OnClick(R.id.id_user_reg_btn)
	public void goToReg() {
		AccountRegFragment.go(getActivity());
	}

	@OnClick(R.id.id_user_login_btn)
	public void goToLogin() {
		AccountLoginFragment.go(getActivity());
	}

	@OnClick(R.id.id_user_exit_btn)
	public void logout() {
		AccessTokenKeeper.clear(getActivity());
		loginActionLayout.setVisibility(View.VISIBLE);
		logoutActionLayout.setVisibility(View.GONE);
		ViewUtils.toast("退出成功");
	}

	public static void go(Activity act) {
		MyPageHelper.account.showMyPage(act);
	}

}
